/**
 * Created by lixiaodi on 2018/9/27 0027.
 */
var states = [{code: 1, text: '开启' }, {code: 2, text: '关闭'}];
mini.parse();
mini_debugger = false;
var grid = mini.get("datagrid1");
//grid.load(ajaxUrl + "/user/list.do");
//grid.load();
var url = bootPATH + "../frame/frame1/pages/";      //路径前缀
var menu = new ColumnsMenu(grid);
//页面初始化
$(function(){
	httpRequest();
});
//增加
function add() {
    mini.open({
        url: url +"alertBox/userWindow.html",
        title: "新增用户", width: 800, height: 700,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: "new"};
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
        	httpRequest();
        }
    });
}
//编辑
function edit() {
    var row = grid.getSelected();
    console.log(row);
    if (row) {
        mini.open({
            url: url +"alertBox/userWindow.html",
            title: "编辑用户", width: 800, height: 700,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", id: row.userId,row:row};
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
            	httpRequest();
            }
        });

    } else {
        mini.alert("请选中一条记录");
    }

}
//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        mini.confirm("确定删除选中记录？","系统提示",function(action){
            if(action == "ok"){
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.userId);
                }
                var id = ids.join(',');
                grid.loading("操作中，请稍后......");
                $.ajax({
                    url: ajaxUrl + "/user/deleteListUserId.do?userIdList=" + id,
                    type:"post",
                    success: function (data) {
                        if(data.state == 'error'){
                        	grid.unmask();
                        	mini.alert(data.message);
                        }else{
                        	httpRequest();
                        }
                    },
                    error: function () {
                        mini.alert('失败');
                    }
                });
            }
        });
    } else {
        mini.alert("请选中一条记录");
    }
}
//查询
function search() {
    var key = mini.get("key").getValue();
    //grid.load({ key: key });
    $.ajax({
        url: ajaxUrl + "/DepartmentController/getListByParam.do",
        type:"post",
        datatype:"json",
        data:{
            departmentName:key
        },
        success: function (data) {
            grid.setData(data.data)
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
function onKeyEnter(e) {
    search();
}
//刷新
function refresh(){
	httpRequest();
}
//数据请求
function httpRequest(){
	$.ajax({
        url: ajaxUrl + "/user/list.do",
        type:"post",
        success: function (data) {
            console.log(data);
            // userId=da
        	var userData = data.data;
        	//日期格式化
        	for(var i = 0; i < userData.length; i++){
        		//创建日期
        		if(userData[i].createDate){
        			userData[i].createDate = formatDate(userData[i].createDate);
        		}else{
        			userData[i].createDate = '-';
        		}
        		//登陆日期
        		if(userData[i].loginDate){
        			userData[i].loginDate = formatDate(userData[i].loginDate);
        		}else{
        			userData[i].loginDate = '-';
        		}
        		//修改日期
        		if(userData[i].updateDate){
        			userData[i].updateDate = formatDate(userData[i].updateDate);
        		}else{
        			userData[i].updateDate = '-';
        		}
        		//用户状态
        		if(userData[i].userState){
        			userData[i].userState = userData[i].userState == 1 ? '账号开启' : '账号关闭';
        		}else{
        			userData[i].userState = '';
        		}
        	}
        	grid.setData(userData);

        },
        error:function(data){
            mini.alert(data.message);
        }
    });
}
//模糊查询
function userInquire() {
    var searchKey=mini.get("searchKey").getValue();
    console.log(searchKey);
    $.ajax({
        url: ajaxUrl + "/user/findLike.do",
        type:"POST",
        data:{
            Name:searchKey
        },
        success:function (data) {
            console.log(data);
            grid.setData(data);
        },
        error:function (data) {
            alert("7")
        }

    })
}
//回车模糊查询
function onKeyEnter() {
    userInquire();
}